package org.hot100_2;

import org.leetcode.TreeNode;

import java.util.*;

public class leetcode102 {
    public List<List<Integer>> levelOrder(TreeNode root) {
        List<List<Integer>> res= new ArrayList<>();
        print(root,res);
        return res ;
    }

    private void print(TreeNode root, List<List<Integer>> res){
        if (root==null){
            return ;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(root);

        while (!queue.isEmpty()){
            int size = queue.size();
            List<Integer> item = new ArrayList<>();
            while (size>0){
                TreeNode node = queue.poll();
                item.add(node.val);
                if (node.left!=null){
                    queue.add(node.left);
                }

                if (node.right!=null){
                    queue.add(node.right);
                }
                size--;
            }

            res.add(item);
        }
    }
}
